#------------------------------------------------------------------------------------------------#
# Replication Code: Diagnostics
# Article: How Men React to Shifts in Gender Representation: Ignored Interests? Good for Society?
# Journal: Political Research Quarterly
# Author: Verena Reidinger
# July 25
#------------------------------------------------------------------------------------------------#

#---------------------------------#
####     0. Preparations       ####
#---------------------------------#

# loading packages
library(cregg)
library(lme4)

#--------------------------------------------------------------------------#
####     1. Appendix 2:Group Shares in the Sample; Tables A2-A5         ####
#--------------------------------------------------------------------------#

# load datasets for analysis of male respondents
load("sample_dat_all.RData") # respondents level data for gender, ethnicity, levels of education, and age group

#Sample Gender Distribution in TABLE A2:
TA2 <- round(prop.table(table(sample$GENDER))*100, 1)
print(TA2)

#For tables A3-A5 I use the male sub-sample
male_sample <- sample %>% 
  filter(GENDER == "Male")

#Table A3 shows the distribution of ethnicities in the male sub-sample
#To compare the categorisation of ethnicities with census data I combine the two "White" categories: 
# 1. "White: English, Welsh, Scottish, Northern Irish or British " 83.0 %  and "Other White (E.g. European)" 3.7 %; = 86.70 %
TA3 <- round(prop.table(table(male_sample$ETHNICITY))*100, 1)
print(TA3)

#Table A4 shows the distribution of educational levels in the male sub-sample
TA4 <- round(prop.table(table(male_sample$EDUCATION_LEVEL))*100, 1)
print(TA4)

#Table A5 shows the distribution of age groups 
TA5 <- round(prop.table(table(male_sample$AGEGROUP))*100, 1)
print(TA5)

#------------------------------------------------------------------------------#
####  2. Appendix 3: Diagnostics; Frequency of attribute levels; Table A6   ####
#------------------------------------------------------------------------------#

# load rating and respondent data to run diagnostics
load("resp_dat_male.RData") # respondent level data of male resp; named "mal"
load("resp_dat_female.RData") # respondent level data of female resp; named "fem"
load("out_dat.RData") # rating level data for output expectations, named "o"
load("fair_dat.RData") # rating level data for fairness perceptions, named "f"

# Frequencies are listed for women and men separately

# merge "mal" and "o"/"f" 
mal_o <- merge(mal, o, by = "ResponseId", all.x = T) 
mal_f <- merge(mal, f, by = "ResponseId", all.x = T) 
# merge "fem" and "o"/"f" 
fem_o <- merge(fem, o, by = "ResponseId", all.x = T) 
fem_f <- merge(fem, f, by = "ResponseId", all.x = T) 

# Calculate frequencies for female respondents and ratings of output expectations; using the cregg package
frequ_fo <- fem_o %>% 
  cj_freqs(~ gender + age + class + ethnicity + ideology, 
           id = ~ ResponseId) %>% 
  select(-c("statistic", "feature")) %>% 
  rename("Female - O" = "estimate")

# Calculate frequencies for female respondents and ratings of fairness perceptions; using the cregg package
frequ_ff <- fem_f %>% 
  cj_freqs(~ gender + age + class + ethnicity + ideology, 
           id = ~ ResponseId) %>% 
  select(-c("statistic", "feature")) %>% 
  rename("Female - F" = "estimate")

# Calculate frequencies for male respondents and ratings of output expectations; using the cregg package
frequ_mo <- mal_o %>% 
  cj_freqs(~ gender + age + class + ethnicity + ideology, 
           id = ~ ResponseId) %>% 
  select(-c("statistic", "feature")) %>% 
  rename("Male - O" = "estimate")

# Calculate frequencies for male respondents and ratings of fairness perceptions; using the cregg package
frequ_mf <- mal_f %>% 
  cj_freqs(~ gender + age + class + ethnicity + ideology, 
           id = ~ ResponseId) %>% 
  select(-c("statistic", "feature")) %>% 
  rename("Male - F" = "estimate")

#Generate Table A6
freq_table <- cbind(frequ_mo, frequ_fo, frequ_mf, frequ_ff) %>% 
              select("level", "Male - O", "Female - O", "Male - F", "Female - F")
                    rowid_to_column("Nr.")

# TABLE A6
print(freq_table)

#--------------------------------------------------------------------------#
####       3. Appendix 3: Diagnostics; Task Effects; Figure A1          ####
#--------------------------------------------------------------------------#

# Task effects are calculated with the cregg package; 
# the task variable is changed to a factor
mal_o$task <- as.factor(mal_o$task)

# estimate marginal means by tasks
mm_task <- cj(mal_o, REP_RAT ~ gender + age + class + ethnicity + ideology, 
            id = ~ResponseId, estimate = "mm", by = ~task)
# plot the marginal means
task_mal_o <- plot(mm_task, group = "task") + ggtitle("Output Expectations")
task_mal_o

# the task variable is changed to a factor
mal_f$task <- as.factor(mal_f$task)

# estimate marginal means by tasks
mm_task <- cj(mal_f, COND_RAT ~ gender + age + class + ethnicity + ideology, 
              id = ~ResponseId, estimate = "mm", by = ~task)
# plot the marginal means
task_mal_f <- plot(mm_task, group = "task") + ggtitle("Fairness Perceptions")
task_mal_f

# Combine the two figures for Figure A1
FIGA1 <- ggarrange(task_mal_o, task_mal_f)
#FIGURE A1
FIGA1

ggsave("Output_Figures/FIGA1.png", width = 33, height = 22, scale = 0.95, units = "cm", bg = "white")

#--------------------------------------------------------------------------#
####       4. Appendix 3: Diagnostics; Order Effects; Figure A2          ####
#--------------------------------------------------------------------------#

# Load the data of all respondents 
load("resp_dat_all.RData") # respondent level data 

# merge "all" and "o" to estimate models on all respondents' output expectations
all_o <- merge(all, o, by = "ResponseId", all.x = T) 

# change the location of the gender attribute in the experiment to a factor
all_o$gender_loc <- as.factor(all_o$gender_loc)

# Fit model with interaction of the gender attribute and its position
attor_model <- lmer(REP_RAT ~ gender*gender_loc + (1 | ResponseId), data=all_o)

# Plot with the emmip function
order_gender <- emmip(attor_model, gender_loc ~ gender, CIs=TRUE) + 
  theme_minimal() + 
  # define the styling of lines, points and text
  theme(axis.line.x = element_line(linewidth = 0.2, colour = "black"),
        axis.line.y = element_line(linewidth = 0.2, colour = "black"),
        axis.ticks =  element_line(linewidth = 0.3, colour = "black"), 
        axis.text.x = element_text(colour = "black"),
        axis.title.x = element_blank(), 
        plot.title = element_text(hjust = 0.5)) + 
  guides(color=guide_legend(title="Position of\nAttribute")) + 
  ggtitle("Order Effects") + 
  scale_x_discrete(labels = c("20% Women\n80% Men", 
                              "50% Women\n50% Men", "65% Women\n35% Men"))
  

# FIGURE A2
order_gender

ggsave("Output_Figures/FIGA2.png", width = 33, height = 22, scale = 0.95, units = "cm", bg = "white")










